package com.zd.core.dao.template;

import java.util.Arrays;
import java.util.List;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;

import com.zd.core.dao.entity.Page;

public abstract class BaseJdbcTemplate<T> {
	
	/**
	 * 日志
	 */
	private Logger logger = LoggerFactory.getLogger(BaseJdbcTemplate.class);
	//http://www.html.org.cn/books/springReference/ch11s05.html
	/**
	 * jdbc模板
	 */
	private JdbcTemplate jdbcTemplate = new JdbcTemplate();
	/**
	 * 数据源
	 */
	private DataSource dataSource;

	public List<T> findListBean(String sql, Class<T> entityClass, Object... values) {		
		RowMapper<T> rowMapper = ParameterizedBeanPropertyRowMapper.newInstance(entityClass);
		logger.debug(sql);
		logger.debug("WHERE-参数 " + Arrays.toString(values));
		return jdbcTemplate.query(sql, rowMapper, values);
	}
	
	//虚方法
	
	/**
	 * 分页
	 */
	public abstract Page<T> findListBeanByPage(Page<T> page, String sql, Class<T> entityClass, Object... values);

	
	//get set
	public JdbcTemplate getSpringJdbcTemplate() {
		return jdbcTemplate;
	}
	
	public DataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
		this.jdbcTemplate.setDataSource(dataSource);
	}
}
